<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="24">
					<!-- <el-form-item label="退租企业" prop="subject">
						<el-input v-model="form.subject" placeholder="事项" clearable maxlength="40"></el-input>
					</el-form-item> -->
                    <el-form-item label="收费企业" prop="enterprise_id"  >
						<el-select v-model="form.enterprise_id" class="m-2" placeholder="企业名称" style="width: 100%;" collapse-tags collapse-tags-tooltip :max-collapse-tags="3" clearable>
							<el-option
								v-for="item in epList"
								:key="item.enterprise_id"
								:label="item.enterprise_id"
								:value="item.enterprise_id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="24">
					<!-- <el-form-item label="退租企业" prop="subject">
						<el-input v-model="form.subject" placeholder="事项" clearable maxlength="40"></el-input>
					</el-form-item> -->
                    <el-form-item label="收费标题" prop="charge_title"  >
						<el-input v-model="form.charge_title" placeholder="请输入" clearable maxlength="180"></el-input>
					</el-form-item>
				</el-col>
				<sc-form-table v-model="chargeList" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
			
				<el-table-column prop="charge_item" label="收费项目"  :show-overflow-tooltip="true">
					<template #default="scope">
						<el-input v-model="scope.row.charge_item" placeholder="请输入" clearable maxlength="180"></el-input>
					</template>					
				</el-table-column>
				<el-table-column prop="charge_unit" label="单位" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						<el-input v-model="scope.row.charge_unit" placeholder="请输入" clearable maxlength="180"></el-input>
					</template>	
				</el-table-column>
				<el-table-column prop="charge_price" label="单价"  align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						<el-input v-model="scope.row.charge_price" placeholder="请输入" clearable maxlength="180"></el-input>
					</template>
				</el-table-column>
				<el-table-column prop="charge_num" label="数量"  align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						<el-input v-model="scope.row.charge_num" placeholder="请输入" clearable maxlength="180"></el-input>
					</template>
				</el-table-column>
				<el-table-column prop="total_price" label="总价" width="100" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						<el-input v-model="scope.row.total_price" placeholder="请输入" clearable maxlength="180"></el-input>
					</template>
				</el-table-column>
			</sc-form-table>
				<el-col :lg="24">
					<el-form-item label="时间" prop="create_time"  style="width: 40%;margin-top: 15px;">
						<el-date-picker v-model="form.create_time" :disabled-date="publishDateAfter" placeholder="收费日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
                <el-col :lg="24" >
					<el-form-item label="交费单据" prop="attachment_upload">
						<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'ici_charge'}" :showFileList="false" @success="addNewFile">
							<attach-menu v-model:attachIds="form.attachment_upload" :roleFlag="4" :preview="true"></attach-menu>
							<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
                </sc-upload-file>
					</el-form-item>
				</el-col>
				<el-col :lg="24">
					<el-form-item label="负责人" prop="user_join">
						<select-user-dialog v-model:accountId="form.user_join" :mode="true"/>
					</el-form-item>
				</el-col>
			</el-row>

		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

    <el-dialog :title="Title" :visible.sync="open" width="600px" append-to-body
           :close-on-click-modal=false>
</el-dialog>


</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
import { style } from "d3";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			addTemplate: {
				charge_item: "",
				charge_unit: "",
				charge_price: "",
				charge_num: "",
				total_price: "",
			},
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			floorList:[],
			roomList:[],
			deviceList:[],
			chargeList:[],
			mode: "add",
			titleMap: {
				add: '收费登记',

			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templates: [],
			epList:[],
			form: {
				id: '',
				enterprise_id: '',
				charge_title: '',
				charge_content: '',
				create_time: '',
				attachment_upload: '',
				charge_id: ''
			},
			spaceOptions: [],
			spaceOriginOptions: [],
			formSpace:{},
			formPackage:{},
			formPS:{},
			rules: {
				subject: [
					{required: true, message: '预约摘要不能为空', trigger: 'blur'}
				]
			}
            ,
		}
    

	},
	mounted() {
		this.getEPList();
	},
	methods: {
		async remoteMethod(keyword) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
        handleUpload(){

        },
		async getEPList()
		{
			var res = await this.$API.attractinvestment.checkin.getCheckinList.get();
			if (res.code == 200) {
				this.epList = res.data.list;
			}
		},
		isValid(data){
			if (Object.prototype.toString.call(data) === '[object Undefined]') {
				return false
			}
			if (Object.prototype.toString.call(data) === '[object Null]') {
				return false
			}
			return true
		},
		async getSpaceListByBuildingId() {
			if(!this.isValid(this.form.building_id)){
				setTimeout(()=>{
					this.$alert('您尚未选择楼宇', '提示', {type: 'error'})
				}, 1000)
				return false
			}
			let res = await this.$API.attractinvestment.building.getSpaceByBuildingId.get({building_id: this.form.building_id});
			if (res.code == 200) {
				this.spaceOptions = res.data;
				this.spaceOriginOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteSpaceMethod(keyword) {
			console.log(this.form.building_id)
			// const building_id = this.isValid(this.form.building_id) ? this.form.building_id : 12; //开发模式
				// 如果keyword开头为0，则删除开头多余的0
			if(keyword.startsWith('0')){
				keyword = keyword.substr(1);
			}
			if(keyword.length>0){
				console.log('keyword is ', keyword)
				this.spaceOptions = this.spaceOriginOptions.filter(e=>{return e.room.includes(keyword)})
			}else{
				console.log('keyword is null')
				this.spaceOptions = this.spaceOriginOptions;
			}
		},
		setOtherValue(row,index)
		{
			this.spaceOptions.forEach(item=>{
				if(item.id==row.space_id)
				{
					row.space_name = item.room;
					row.park_name = item.park_name;
					row.area_name = item.area_name;
					row.building_name = item.building_name;
					row.floor_name = item.floor_name;
					row.rental_pricing = item.rental_pricing;
					row.room_size = item.room_size;
				}
			})

		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.charge_content = JSON.stringify(this.chargeList)
					var res;
					if (this.mode == "add") {
						res = await this.$API.attractinvestment.chargeManage.createCharge.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.attractinvestment.chargeManage.updateCharge.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async getFloorList()
		{
			var res = await this.$API.attractinvestment.floor.getFloorList.get({'building_id': this.form.building_id});
			if (res.code == 200) {
				this.floorList = res.data.list;
			}
		},
		async getCanUseDeviceList()
		{
			var res = await this.$API.office.meeting.getCanUseDeviceList.get();
			if (res.code == 200) {
				this.deviceList = res.data;
			}
		},
		setCreateData(data,roomList)
		{
			this.form.building_id = data.building.id;
			this.roomList = roomList;
			this.form.roomId = data.roomId;
			this.form.begin_time = data.begin_time;
			this.form.end_time = data.end_time;
			this.form.id = data.id
			this.form.charge_id = data.charge_id
			this.getSpaceListByBuildingId();
		},
		setData(data,roomList) {
			this.roomList = roomList;
			this.form.enterprise_id = data.enterprise_id
			this.form.charge_title = data.charge_title
			this.form.charge_content = data.charge_content
			this.form.create_time = data.create_time
			this.form.attachment_upload = data.attachment_upload
			this.chargeList = JSON.parse(data.charge_content)
			this.form.id = data.id
			this.form.charge_id = data.charge_id
			this.form.user_join = data.user_join;
		},
		addNewFile(res, file) {
			let tArr = [];
			if (this.form.attachment_upload !== undefined && this.form.attachment_upload != null && this.form.attachment_upload !== '') {
				tArr = this.form.attachment_upload.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachment_upload = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
